<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\Common\Constants\PermissionTypeConstants;
use App\Common\Service\CommonRoleService;
use App\CustHub\Mapper\ChMenuMapper;
use App\CustHub\Mapper\ChUserMapper;
use Hyperf\Di\Annotation\Inject;
use Mine\Abstracts\AbstractService;
use Mine\MineCollection;
use function Hyperf\Collection\collect;

class ChMenuService extends AbstractService
{
    public $mapper;

    #[Inject]
    protected ChUserMapper $userMapper;

    #[Inject]
    protected CommonRoleService $roleService;

    public function __construct(
        ChMenuMapper $mapper
    )
    {
        $this->mapper = $mapper;
    }

    public function getSelectTreeByUser(array $params): array
    {
        $user = $this->userMapper->findById($params['main_uid']);
        $userRoleIds = $user->roles()->pluck('role_id')->toArray();

        $roleMenus = $this->roleService->getMenuByRoleIds($userRoleIds);
        $menuIds = [];
        foreach ($roleMenus as $roleMenu){
            $menuIds = array_merge($menuIds, array_column($roleMenu['menus'],'id'));
        }
        $params['menu_ids'] = $menuIds;
        return $this->mapper->getSelectTree($params);

    }

    public function getMenuIds(int $mainUid): array
    {
        $user = $this->userMapper->findById($mainUid);
        $userRoleIds = $user->roles()->pluck('role_id')->toArray();

        $roleMenus = $this->roleService->getMenuByRoleIds($userRoleIds);
        $menuIds = [];
        foreach ($roleMenus as $roleMenu){
            $menuIds = array_merge($menuIds, array_column($roleMenu['menus'],'id'));
        }
        return array_unique($menuIds);
    }

    public function getSuperAdminRouters($permissionType = PermissionTypeConstants::CUST_HUB,?int $isGuide = null): array
    {
        return $this->sysMenuToRouterTree($this->mapper->getSuperAdminRouters($permissionType,$isGuide));
    }

    public function getRoutersByIds(array $ids,?int $isGuide = null): array
    {
        return $this->sysMenuToRouterTree($this->mapper->getRoutersByIds($ids,$isGuide));
    }

    /**
     * 系统菜单转前端路由树
     * @return array
     */
    public function sysMenuToRouterTree(array $data): array
    {
        if (empty($data)) return [];
        $routers = [];
        foreach ($data as $menu) {
            $routers[] = $this->setRouter($menu);
        }
        return (new MineCollection())->toTree($routers);
    }

    /**
     * @param $menu
     * @return array
     */
    public function setRouter(&$menu): array
    {
        $route = ($menu['type'] == 'L' || $menu['type'] == 'I') ? $menu['route'] : '/' . $menu['route'];

        $result = [
            'id' => $menu['id'],
            'parent_id' => $menu['parent_id'],
            'name' => $menu['code'],
            'component' => $menu['component'],
            'path' => $route,
            'redirect' => $menu['redirect'],
            'meta' => [
                'type'   => $menu['type'],
                'icon'   => $menu['icon'],
                'img'    => $menu['img'],
                'desc'   => $menu['desc'],
                'doc_url'=> $menu['doc_url'],
                'title'  => $menu['name'],
                'hidden' => ($menu['is_hidden'] === 1),
                'hiddenBreadcrumb' => false
            ],
            'tips' => empty($menu['remark']) ? '' : $menu['remark']
        ];

        if(!empty($menu['props'])){
            $props = explode(PHP_EOL,$menu['props']);
            $newProps = [];
            foreach ($props as $item){
                $arr = explode(':',$item);
                if(isset($arr[0]) || isset($arr[1])){
                    $newProps[$arr[0]] = $arr[1];
                }
            }
            $result['props'] = $newProps;
        }

        return $result;
    }
}